package org.hot100_6.leetcode19;

import org.algorithm.List.ListNode;
import org.utils.ListNode.ListNodeUtils;

public class Solution {

    public static void main(String[] args) {
        int [] temp = {1};

        ListNode listNode = ListNodeUtils.createListNode(temp);

        ListNode  result= removeNthFromEnd(listNode, 1);
        ListNodeUtils.Print(result);
    }

    public static ListNode removeNthFromEnd(ListNode head, int n) {

        ListNode dummy = new ListNode(-1);
        dummy.next = head;

        ListNode first = dummy;
        ListNode slow = dummy;

        while (n>=0){
            n--;
            first = first.next;
        }

        while (first!=null){
            first = first.next;
            slow = slow.next;
        }

        slow.next = slow.next.next;

        return dummy.next;
    }
}
